package com.hy.controller;


import com.hy.admin.pojo.User;
import com.hy.dto.LoginDto;
import com.hy.service.SysMenuService;
import com.hy.service.UserService;
import com.hy.service.ValidateCodeService;
import com.hy.system.LoginVo;
import com.hy.system.ValidateCodeVo;
import com.hy.util.AuthContextUtil;
import com.hy.util.common.Result;
import com.hy.util.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private UserService userService ;
    @Autowired
    private ValidateCodeService validateCodeService;


    /**
     * 登录
     * @param loginDto
     * @return
     */
    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping(value = "/getUserInfo")
    public Result<User> getUserInfo() {
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }

    /**
     * 生成验证码
     * @return
     */
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 退出
     * @param token
     * @return
     */
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        userService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


}
